package com.daocao.yfb.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.daocao.yfb.R;
import com.daocao.yfb.adapter.MsgTipsItemAdapter;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.NoticeListBean;
import com.daocao.yfb.api.model.NoticeListModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.view.OnLoadeMoreListener;
import com.daocao.yfb.view.V19FrameLayout;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MsgItemTipsActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private Unbinder unbiner;
    private MsgTipsItemAdapter adapter;
    private String type;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getNoticeList(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_msg_tips);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        type = getIntent().getStringExtra("type");
        if (TextUtils.isEmpty(type)) {
            return;
        }
        if (type.equals("1")) {
            txtTitle.setText("订单消息");
        } else if (type.equals("2")) {
            txtTitle.setText("系统消息");
        }
        adapter = new MsgTipsItemAdapter(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(listener);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getNoticeList(1);
            }
        });
        getNoticeList(1);
    }

    public void getNoticeList(int page) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        NoticeListBean bean = new NoticeListBean();
        bean.type = type;
        bean.current_page = page;
        Api.api_service.getNoticeList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas,page);
                });

    }

    private void setData(List<NoticeListModel.DatasBean> datas,int page) {
        adapter.add(datas,page==1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
